package com.laisheng.interview.controller;

import com.laisheng.interview.model.Evaluation;
import com.laisheng.interview.service.EvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 面试评价控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/evaluation")
@CrossOrigin(origins = "*")
public class EvaluationController {

    @Autowired
    private EvaluationService evaluationService;

    /**
     * 保存或更新评价
     */
    @PostMapping("/save")
    public Map<String, Object> saveEvaluation(@RequestBody Map<String, Object> request) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            Long accountingId = Long.valueOf(request.get("accountingId").toString());
            String content = (String) request.get("content");
            String rating = (String) request.get("rating");
            
            if (content == null || content.trim().isEmpty()) {
                result.put("success", false);
                result.put("message", "评价内容不能为空");
                return result;
            }
            
            boolean success = evaluationService.saveOrUpdateEvaluation(accountingId, content, rating);
            
            if (success) {
                result.put("success", true);
                result.put("message", "评价保存成功");
                log.info("评价保存成功，面试者ID: {}", accountingId);
            } else {
                result.put("success", false);
                result.put("message", "评价保存失败");
                log.error("评价保存失败，面试者ID: {}", accountingId);
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "评价保存异常: " + e.getMessage());
            log.error("评价保存异常", e);
        }
        
        return result;
    }

    /**
     * 根据面试者ID查询评价
     */
    @GetMapping("/{accountingId}")
    public Map<String, Object> getEvaluation(@PathVariable Long accountingId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            Evaluation evaluation = evaluationService.getByAccountingInfoId(accountingId);
            
            if (evaluation != null) {
                result.put("success", true);
                result.put("data", evaluation);
            } else {
                result.put("success", false);
                result.put("message", "未找到评价信息");
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询评价异常: " + e.getMessage());
            log.error("查询评价异常", e);
        }
        
        return result;
    }

    /**
     * 删除评价
     */
    @DeleteMapping("/{accountingId}")
    public Map<String, Object> deleteEvaluation(@PathVariable Long accountingId) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean success = evaluationService.deleteByAccountingInfoId(accountingId);
            
            if (success) {
                result.put("success", true);
                result.put("message", "评价删除成功");
                log.info("评价删除成功，面试者ID: {}", accountingId);
            } else {
                result.put("success", false);
                result.put("message", "评价删除失败");
                log.error("评价删除失败，面试者ID: {}", accountingId);
            }
            
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "评价删除异常: " + e.getMessage());
            log.error("评价删除异常", e);
        }
        
        return result;
    }
} 